#!/usr/bin/env php
<?php
/* Script to automatically create contact form. Information
 * is sent via email and logged into a table.
 *
 * @author Nesbert Hidalgo
 * @version 1
 */

// include environment creovel and libraries
include_once( dirname(dirname(__FILE__)) . DIRECTORY_SEPARATOR . 'config/environment.php' );
define(SCRIPTS_TEMPLATE_PATH, SCRIPT_PATH . 'templates/contact_form/');

echo "Creating contact form...\n";

// mode and database check
if ( $_ENV['mode'] !== 'development' ) {
	echo "Error: Script can only be ran in \"development\" mode.\n";
	die;
}
if ( $_ENV[development][database] == 'database' ) {
	echo "Error: Please set database properties (host, datebase, username, password).\n";
	die;
}

// create table
$db = new model();
$db->query("CREATE TABLE IF NOT EXISTS contact_requests (
			id INT(11) NOT NULL AUTO_INCREMENT,
			created_at DATETIME NOT NULL,
			updated_at DATETIME NOT NULL,
			to_email VARCHAR(255) NOT NULL,
			email VARCHAR(255) NOT NULL,
			name VARCHAR(255) NOT NULL,
			subject VARCHAR(255) NOT NULL,
			body TEXT NOT NULL,
			address_1 VARCHAR(100) NOT NULL,
			address_2 VARCHAR(100) NOT NULL,
			city VARCHAR(100) NOT NULL,
			state VARCHAR(2) NOT NULL,
			zip VARCHAR(50) NOT NULL,
			phone VARCHAR(20) NOT NULL,
			fax VARCHAR(20) NOT NULL,
			PRIMARY KEY ( `id` )
			)");

// set permissions for app directory
/*
exec('whoami', $user);
$user = $user[0];
echo "Setting directory and file permissions.\n";
*/

// check if files exists
switch ( false )
{
	// check directories
	case ( !file_exists(VIEWS_PATH . 'contact') ):
	case ( !file_exists(VIEWS_PATH . 'contact_mailer') ):
	// check models
	case ( !file_exists(MODELS_PATH . "contact_request.php") ):
	case ( !file_exists(MODELS_PATH . "contact_mailer.php") ):
	// check models
	case ( !file_exists(CONTROLLERS_PATH . "contact_controller.php") ):
	// check views
	case ( !file_exists(VIEWS_PATH . "contact/form.php") ):
	case ( !file_exists(VIEWS_PATH . "contact/sent.php") ):
	case ( !file_exists(VIEWS_PATH . "contact_mailer/contact.php") ):
		echo "Error: Directory/File already exists can not create contact form files.\n";
		die;
	break;

}

// set variables
switch ( false )
{

	case ( $contact_request = 		file_get_contents(SCRIPTS_TEMPLATE_PATH . 'models/contact_request.php') ):
	case ( $contact_mailer = 		file_get_contents(SCRIPTS_TEMPLATE_PATH . 'models/contact_mailer.php') ):
	case ( $contact_controller = 	file_get_contents(SCRIPTS_TEMPLATE_PATH . 'controllers/contact_controller.php') ):
	case ( $contact_form_view = 	file_get_contents(SCRIPTS_TEMPLATE_PATH . 'views/contact/form.php') ):
	case ( $contact_sent_view = 	file_get_contents(SCRIPTS_TEMPLATE_PATH . 'views/contact/sent.php') ):
	case ( $contact_mailer_view = 	file_get_contents(SCRIPTS_TEMPLATE_PATH . 'views/contact_mailer/contact.php') ):
		echo "Error: An error occurred while retrieving a contact form template.\n";
		die;
	break;

}

// set email address if passed
$email = $_SERVER['argv'][1];
if ( is_email($email) ) {
	$contact_controller = str_replace('email@address.com', $email, $contact_controller);
}

// create directories
mkdir(VIEWS_PATH . 'contact');
mkdir(VIEWS_PATH . 'contact_mailer');

// create files
switch ( false )
{
	// create models
	case (file_put_contents(MODELS_PATH . "contact_request.php", $contact_request)):
	case (file_put_contents(MODELS_PATH . "contact_mailer.php", $contact_mailer)):
	// create controllers
	case ( file_put_contents(CONTROLLERS_PATH . "contact_controller.php", $contact_controller)):
	// create views
	case (file_put_contents(VIEWS_PATH . "contact/form.php", $contact_form_view)):
	case (file_put_contents(VIEWS_PATH . "contact/sent.php", $contact_sent_view)):
	case (file_put_contents(VIEWS_PATH . "contact_mailer/contact.php", $contact_mailer_view) ):
		echo "Error: An error occurred while writing a contact form template. Please make sure you have correct permissions!\n";
		die;
	break;

}

echo "Finished creating contact form!\n";
?>
